import { KEYCODE } from "@/utils";
/**阴影算法 */
export function queryWidgetByShadowAlgorithm(widgets, focusId, _keycode) {
  const currentWidget = widgets[focusId];
  if (!currentWidget) return focusId;
  const currentRect = currentWidget.postion;
  let resultFocusId;
  let dis = Infinity, tempDis;
  for (const key in widgets) {
    if (!Object.prototype.hasOwnProperty.call(widgets, key)) continue;
    const tempWidget = widgets[key];
    if (currentWidget.$context === tempWidget.$context) continue;
    const tempRect = tempWidget.postion;
    if (tempRect.height == 0 || tempRect.width == 0) continue;
    if (_keycode === KEYCODE.LEFT && tempRect.right < currentRect.centerX && tempRect.left < currentRect.left && tempRect.top < currentRect.bottom && tempRect.bottom > currentRect.top) {
      tempDis = currentRect.right - tempRect.right;
      if (tempDis < dis || (tempDis === dis && Math.abs(currentRect.centerY - tempRect.centerY) < Math.abs(currentRect.centerY - widgets[resultFocusId].postion.centerY))) {
        resultFocusId = key;
        dis = tempDis;
      }
    } else if (_keycode === KEYCODE.RIGHT && tempRect.left > currentRect.centerX && tempRect.right > currentRect.right && tempRect.top < currentRect.bottom && tempRect.bottom > currentRect.top) {
      tempDis = tempRect.left - currentRect.left;
      if (tempDis < dis || (tempDis === dis && Math.abs(currentRect.centerY - tempRect.centerY) < Math.abs(currentRect.centerY - widgets[resultFocusId].postion.centerY))) {
        resultFocusId = key;
        dis = tempDis;
      }
    } else if (_keycode === KEYCODE.UP && tempRect.bottom < currentRect.centerY && tempRect.top < currentRect.top && tempRect.left <= currentRect.right && tempRect.right >= currentRect.left) {
      tempDis = currentRect.bottom - tempRect.bottom;
      if (tempDis < dis || (tempDis === dis && Math.abs(currentRect.centerX - tempRect.centerX) < Math.abs(currentRect.centerX - widgets[resultFocusId].postion.centerX))) {
        resultFocusId = key;
        dis = tempDis;
      }
    } else if (_keycode === KEYCODE.DOWN && tempRect.top > currentRect.centerY && tempRect.bottom > currentRect.bottom && tempRect.left < currentRect.right && tempRect.right > currentRect.left) {
      tempDis = tempRect.top - currentRect.top;
      if (tempDis < dis || (tempDis === dis && Math.abs(currentRect.centerX - tempRect.centerX) < Math.abs(currentRect.centerX - widgets[resultFocusId].postion.centerX))) {
        resultFocusId = key;
        dis = tempDis;
      }
    }
  }
  return resultFocusId;
}


/**区域算法 */
export function queryWidgetByAreaAlgorithm(widgets, focusId, _keycode) {
  const currentWidget = widgets[focusId];
  if (!currentWidget) return focusId;
  const currentRect = currentWidget.postion;
  let resultFocusId;
  let dis = Infinity, tempDis;
  for (const key in widgets) {
    if (!Object.prototype.hasOwnProperty.call(widgets, key)) continue;
    const tempWidget = widgets[key];
    if (currentWidget.$context === tempWidget.$context) continue;
    const tempRect = tempWidget.postion;
    if (tempRect.height == 0 || tempRect.width == 0) continue;
    if (_keycode === KEYCODE.LEFT && tempRect.right <= currentRect.left) {
      tempDis = Infinity;
      if (tempRect.bottom < currentRect.top) {
        tempDis = Math.sqrt(Math.pow(currentRect.left - tempRect.right, 2) + Math.pow(currentRect.top - tempRect.bottom, 2));
      } else if (tempRect.top > currentRect.bottom) {
        tempDis = Math.sqrt(Math.pow(currentRect.left - tempRect.right, 2) + Math.pow(tempRect.top - currentRect.bottom, 2));
      } else {
        tempDis = currentRect.left - tempRect.right;
      }
      if (tempDis < dis) {
        resultFocusId = key;
        dis = tempDis;
      }
    } else if (_keycode === KEYCODE.RIGHT && tempRect.left >= currentRect.right) {
      tempDis = Infinity;
      if (tempRect.bottom < currentRect.top) {
        tempDis = Math.sqrt(Math.pow(tempRect.left - currentRect.right, 2) + Math.pow(currentRect.top - tempRect.bottom, 2));
      } else if (tempRect.top > currentRect.bottom) {
        tempDis = Math.sqrt(Math.pow(tempRect.left - currentRect.right, 2) + Math.pow(tempRect.top - currentRect.bottom, 2));
      } else {
        tempDis = tempRect.left - currentRect.right;
      }
      if (tempDis < dis) {
        resultFocusId = key;
        dis = tempDis;
      }
    } else if (_keycode === KEYCODE.UP && tempRect.bottom <= currentRect.top) {
      tempDis = Infinity;
      if (tempRect.right < currentRect.left) {
        tempDis = Math.sqrt(Math.pow(currentRect.top - tempRect.bottom, 2) + Math.pow(currentRect.left - tempRect.right, 2));
      } else if (tempRect.left > currentRect.right) {
        tempDis = Math.sqrt(Math.pow(currentRect.top - tempRect.bottom, 2) + Math.pow(tempRect.left - currentRect.right, 2));
      } else {
        tempDis = currentRect.top - tempRect.bottom;
      }
      if (tempDis < dis) {
        resultFocusId = key;
        dis = tempDis;
      }
    } else if (_keycode === KEYCODE.DOWN && tempRect.top >= currentRect.bottom) {
      tempDis = Infinity;
      if (tempRect.right < currentRect.left) {
        tempDis = Math.sqrt(Math.pow(tempRect.top - currentRect.bottom, 2) + Math.pow(currentRect.left - tempRect.right, 2));
      } else if (tempRect.left > currentRect.right) {
        tempDis = Math.sqrt(Math.pow(tempRect.top - currentRect.bottom, 2) + Math.pow(tempRect.left - currentRect.right, 2));
      } else {
        tempDis = tempRect.top - currentRect.bottom;
      }
      if (tempDis < dis) {
        resultFocusId = key;
        dis = tempDis;
      }
    }
  }
  return resultFocusId
}